// RoiRectangle.qml
import QtQuick 2.15
import QtQuick.Controls 2.15


Item {
    id: root
    property real borderWidth: 1
    property color borderColor: "blue"
    property color handleColor: "white"
    property color handleBorderColor:"black"
    property real handleSize: 5
    property real sensitivity: 20
    property real distance:80

    width: 200
    height: 200
    // Border rectangle (invisible, just for layout purposes)
      Rectangle {
          id: borderRect
          anchors.fill: parent
          color: "#000000FF"
          border.color: borderColor
          border.width: borderWidth
          MouseArea{
              anchors.fill: parent
              drag.target: root
              drag.axis: Drag.XAndYAxis
              hoverEnabled: true

              onEntered: {
                  cursorShape=Qt.SizeAllCursor
              }
              onExited: {
                  cursorShape=Qt.ArrowCursor
              }
              onPressed: {
                  cursorShape=Qt.SizeAllCursor
              }
              onReleased: {
                  cursorShape=Qt.SizeAllCursor
              }
          }
      }

      // Top-left corner handle
      Rectangle {
          id:topLeft
          x:borderRect.x-handleSize/2
          y:borderRect.y-handleSize/2
          z:borderRect.z+1
          border{
            color:handleBorderColor
            width:1
          }
          width: handleSize
          height: handleSize
          color: handleColor
          radius: handleSize / 2
      }
      MouseArea{
        id:topLeftMouseArea
        x:topLeft.x-sensitivity
        y:topLeft.y-sensitivity
        z:topLeft.z+1
        width:topLeft.width+2*sensitivity
        height:topLeft.height+2*sensitivity
        property  point originalPos

        onEntered: {
            cursorShape=Qt.SizeFDiagCursor
        }
        onExited: {
            cursorShape=Qt.ArrowCursor
        }
        onPressed: (mouse)=>{
            cursorShape =Qt.SizeFDiagCursor
            originalPos={x:mouseX,y:mouseY}

        }
        onReleased:(mouse)=> {

        }
        onPositionChanged:(mouse)=> {
            cursorShape=Qt.SizeFDiagCursor
            if(mouse.buttons&Qt.LeftButton){
                var deltaX=mouseX-originalPos.x
                var deltaY=mouseY-originalPos.y
                root.x+=deltaX
                root.y+=deltaY
                root.width-=2*deltaX//*Math.cos(-root.rotation/90*Math.asin(1)))
                root.height-=2*deltaY//*Math.sin(root.rotation/90*Math.asin(1)))
            }
        }
      }


      // Top-right corner handle
      Rectangle {
          id:topRight
          x:borderRect.x+borderRect.width-handleSize/2
          y:borderRect.y-handleSize/2
          border{
            color:handleBorderColor
            width:1
          }
          width: handleSize
          height: handleSize
          color: handleColor
          radius: handleSize / 2
      }
      MouseArea{
        id:topRightMouseArea
        x:topRight.x-sensitivity
        y:topRight.y-sensitivity
        z:topRight.z+1
        width:topRight.width+2*sensitivity
        height:topRight.height+2*sensitivity
        property  point originalPos

        onEntered: {
            cursorShape=Qt.SizeBDiagCursor
        }
        onExited: {
            cursorShape=Qt.ArrowCursor
        }
        onPressed: (mouse)=>{
            cursorShape =Qt.SizeBDiagCursor
            originalPos={x:mouseX,y:mouseY}
        }
        onReleased:(mouse)=> {

        }
        onPositionChanged:(mouse)=> {
            cursorShape=Qt.SizeBDiagCursor
            if(mouse.buttons&Qt.LeftButton){
                var deltaX=mouseX-originalPos.x
                var deltaY=mouseY-originalPos.y
                root.x-=deltaX
                root.y+=deltaY
                root.width+=2*deltaX
                root.height-=2*deltaY
            }
        }
      }








      // Bottom-right corner handle
      Rectangle {
          id:bottomRight
          x:borderRect.x+borderRect.width-handleSize/2
          y:borderRect.y+borderRect.height-handleSize/2
          border{
            color:handleBorderColor
            width:1
          }
          width: handleSize
          height: handleSize
          color: handleColor
          radius: handleSize / 2
      }
      MouseArea{
        id:bottomRightMouseArea
        x:bottomRight.x-sensitivity
        y:bottomRight.y-sensitivity
        z:bottomRight.z+1
        width:bottomRight.width+2*sensitivity
        height:bottomRight.height+2*sensitivity
        property  point originalPos

        onEntered: {
            cursorShape=Qt.SizeFDiagCursor
        }
        onExited: {
            cursorShape=Qt.ArrowCursor
        }
        onPressed: (mouse)=>{
            cursorShape =Qt.SizeFDiagCursor
            originalPos={x:mouseX,y:mouseY}
        }
        onReleased:(mouse)=> {

        }
        onPositionChanged:(mouse)=> {
            cursorShape=Qt.SizeFDiagCursor
            if(mouse.buttons&Qt.LeftButton){
                var deltaX=mouseX-originalPos.x
                var deltaY=mouseY-originalPos.y
                root.y-=deltaY
                root.x-=deltaX
                root.width+=2*deltaX
                root.height+=2*deltaY
            }
        }
      }


      // Bottom-left corner handle
      Rectangle {
          id:bottomLeft
          x:borderRect.x-handleSize/2
          y:borderRect.y+borderRect.height-handleSize/2
          border{
            color:handleBorderColor
            width:1
          }
          width: handleSize
          height: handleSize
          color: handleColor
          radius: handleSize / 2
      }

      MouseArea{
        id:bottomLeftMouseArea
        x:bottomLeft.x-sensitivity
        y:bottomLeft.y-sensitivity
        z:bottomLeft.z+1
        width:bottomLeft.width+2*sensitivity
        height:bottomLeft.height+2*sensitivity
        property  point originalPos

        onEntered: {
            cursorShape=Qt.SizeBDiagCursor
        }
        onExited: {
            cursorShape=Qt.ArrowCursor
        }
        onPressed: (mouse)=>{
            cursorShape =Qt.SizeBDiagCursor
            originalPos={x:mouseX,y:mouseY}
        }
        onReleased:(mouse)=> {

        }
        onPositionChanged:(mouse)=> {
            cursorShape=Qt.SizeBDiagCursor
            if(mouse.buttons&Qt.LeftButton){
                var deltaX=mouseX-originalPos.x
                var deltaY=mouseY-originalPos.y
                root.x+=deltaX
                root.y-=deltaY
                root.width-=2*deltaX
                root.height+=2*deltaY
            }
        }
      }





      // Top-center handle
      Rectangle {
          id:topCenter
          x:borderRect.x+borderRect.width/2-handleSize/2
          y:borderRect.y-handleSize/2
          border{
            color:handleBorderColor
            width:1
          }
          width: handleSize
          height: handleSize
          color: handleColor
          radius: handleSize / 2
      }

      MouseArea{
        id:topCenterMouseArea
        x:topCenter.x-sensitivity
        y:topCenter.y-sensitivity
        z:topCenter.z+1
        width:topCenter.width+2*sensitivity
        height:topCenter.height+2*sensitivity
        property  point originalPos

        onEntered: {
            cursorShape=Qt.SizeVerCursor
        }
        onExited: {
            cursorShape=Qt.ArrowCursor
        }
        onPressed: (mouse)=>{
            cursorShape =Qt.SizeVerCursor
            originalPos={x:mouseX,y:mouseY}
        }
        onReleased:(mouse)=> {

        }
        onPositionChanged:(mouse)=> {
            cursorShape=Qt.SizeVerCursor
            if(mouse.buttons&Qt.LeftButton){
                var deltaY=mouseY-originalPos.y
                root.y+=deltaY
                root.height-=2*deltaY
            }
        }
      }







      // Bottom-center handle
      Rectangle {
          id:bottomCenter
          x:borderRect.x+borderRect.width/2-handleSize/2
          y:borderRect.y+borderRect.height-handleSize/2
          border{
            color:handleBorderColor
            width:1
          }
          width: handleSize
          height: handleSize
          color: handleColor
          radius: handleSize / 2
      }

      MouseArea{
        id:bottomCenterMouseArea
        x:bottomCenter.x-sensitivity
        y:bottomCenter.y-sensitivity
        z:bottomCenter.z+1
        width:bottomCenter.width+2*sensitivity
        height:bottomCenter.height+2*sensitivity
        property  point originalPos

        onEntered: {
            cursorShape=Qt.SizeVerCursor
        }
        onExited: {
            cursorShape=Qt.ArrowCursor
        }
        onPressed: (mouse)=>{
            cursorShape =Qt.SizeVerCursor
            originalPos={x:mouseX,y:mouseY}
        }
        onReleased:(mouse)=> {

        }
        onPositionChanged:(mouse)=> {
            cursorShape=Qt.SizeVerCursor
            if(mouse.buttons&Qt.LeftButton){
                var deltaY=mouseY-originalPos.y
                root.y-=deltaY
                root.height+=deltaY*2
            }
        }
      }


      // Left-center handle
      Rectangle {
          id:leftCenter
          x:borderRect.x-handleSize/2
          y:borderRect.y+borderRect.height/2-handleSize/2
          border{
            color:handleBorderColor
            width:1
          }
          width: handleSize
          height: handleSize
          color: handleColor
          radius: handleSize / 2
      }
      MouseArea{
        id:leftCenterMouseArea
        x:leftCenter.x-sensitivity
        y:leftCenter.y-sensitivity
        z:leftCenter.z+1
        width:leftCenter.width+2*sensitivity
        height:leftCenter.height+2*sensitivity
        property  point originalPos

        onEntered: {
            cursorShape=Qt.SizeHorCursor
        }
        onExited: {
            cursorShape=Qt.ArrowCursor
        }
        onPressed: (mouse)=>{
            cursorShape =Qt.SizeHorCursor
            originalPos={x:mouseX,y:mouseY}
        }
        onReleased:(mouse)=> {

        }
        onPositionChanged:(mouse)=> {
            cursorShape=Qt.SizeHorCursor
            if(mouse.buttons&Qt.LeftButton){
                var deltaX=mouseX-originalPos.x
                root.x+=deltaX
                root.width-=2*deltaX
            }
        }
      }


      // Right-center handle
      Rectangle {
          id:rightCenter
          x:borderRect.x+borderRect.width-handleSize/2
          y:borderRect.y+borderRect.height/2-handleSize/2
          border{
            color:handleBorderColor
            width:1
          }
          width: handleSize
          height: handleSize
          color: handleColor
          radius: handleSize / 2
      }

      MouseArea{
        id:rightCenterMouseArea
        x:rightCenter.x-sensitivity
        y:rightCenter.y-sensitivity
        z:rightCenter.z+1
        width:rightCenter.width+2*sensitivity
        height:rightCenter.height+2*sensitivity
        property  point originalPos

        onEntered: {
            cursorShape=Qt.SizeHorCursor
        }
        onExited: {
            cursorShape=Qt.ArrowCursor
        }
        onPressed: (mouse)=>{
            cursorShape =Qt.SizeHorCursor
            originalPos={x:mouseX,y:mouseY}
        }
        onReleased:(mouse)=> {

        }
        onPositionChanged:(mouse)=> {
            cursorShape=Qt.SizeHorCursor
            if(mouse.buttons&Qt.LeftButton){
                var deltaX=mouseX-originalPos.x
                root.x-=deltaX
                root.width+=2*deltaX
            }
        }
      }

      // center handle
      Rectangle {
          id:center
          x:borderRect.x+(borderRect.width-1)/2-handleSize/2
          y:borderRect.y+(borderRect.height-1)/2-handleSize/2
          border{
            color:handleBorderColor
            width:1
          }
          width: handleSize
          height: handleSize
          color: handleColor
      }


      // rotated handle
      Rectangle {
          id:rotated
          x:(borderRect.x+borderRect.width)/2-handleSize
          y:(borderRect.y-distance)/2-handleSize
          border{
            color:handleBorderColor
            width:1
          }
          width: handleSize*2
          height: handleSize*2
          color: "#00FF00"
          radius: handleSize*2 / 2
      }

      MouseArea{
        id:rotatedMouseArea
        x:rotated.x-sensitivity
        y:rotated.y-sensitivity
        z:rotated.z+1
        width:rotated.width+2*sensitivity
        height:rotated.height+2*sensitivity
        cursorShape: Qt.ArrowCursor
        property  point originalPos
        onEntered:(mouse) =>{

            cursorShape=Qt.OpenHandCursor
        }
        onExited: {

             cursorShape=Qt.ArrowCursor
        }
        onPressed: (mouse)=>{
            cursorShape=Qt.CloseHandCursor
            originalPos={x:mouseX,y:mouseY}
        }
        onReleased:(mouse)=> {

        }
        onPositionChanged:(mouse)=> {

            if(mouse.buttons&Qt.LeftButton){
                cursorShape=Qt.ClosedHandCursor
                var deltaX=mouseX-originalPos.x
                var deltaY=mouseY-originalPos.y
                var ocX=-root.width/2+rotated.x+originalPos.x
                var ocY=-root.height/2+rotated.y+originalPos.y
                var omX=ocX+deltaX
                var omY=ocY+deltaY
                var deltaAngle=Math.acos((ocX*omX+ocY*omY)/Math.sqrt(ocX*ocX+ocY*ocY)/Math.sqrt(omX*omX+omY*omY))/Math.asin(1)*90
                //叉积判断顺时针逆时针
                if(ocX*omY-ocY*omX<0){
                    deltaAngle=-deltaAngle
                }
                root.rotation+=deltaAngle
            }
        }
      }
}
